import sys
import pyautogui
import time
import pyperclip
import subprocess
import threading
import re
import keyboard
import json
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QComboBox, QTabWidget, QFileDialog, QTextEdit
from PyQt5.QtGui import QFont, QIcon
from baseconvertertab import *
from clipboardmonitortab import *
from intelligentcontrolsystem import *
from ipconvertertab import *


class MouseKeyboardSimulatorTab(QWidget):
    def __init__(self, system):
        super().__init__()
        self.system = system
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        font = QFont()
        font.setPointSize(12)  # 设置字体大小

        self.description_label = QLabel('说明: 该工具可以模拟鼠标和键盘操作。')
        self.description_label.setFont(font)
        layout.addWidget(self.description_label)

        self.mouse_label = QLabel('Mouse Operations:')
        self.mouse_label.setFont(font)
        layout.addWidget(self.mouse_label)

        self.mouse_move_button = QPushButton('Move Mouse to (100, 100)', self)
        self.mouse_move_button.setFont(font)
        self.mouse_move_button.clicked.connect(self.move_mouse)
        layout.addWidget(self.mouse_move_button)

        self.mouse_click_button = QPushButton('Click Mouse at (100, 100)', self)
        self.mouse_click_button.setFont(font)
        self.mouse_click_button.clicked.connect(self.click_mouse)
        layout.addWidget(self.mouse_click_button)

        self.mouse_drag_button = QPushButton('Drag Mouse to (200, 200)', self)
        self.mouse_drag_button.setFont(font)
        self.mouse_drag_button.clicked.connect(self.drag_mouse)
        layout.addWidget(self.mouse_drag_button)

        self.mouse_scroll_button = QPushButton('Scroll Mouse', self)
        self.mouse_scroll_button.setFont(font)
        self.mouse_scroll_button.clicked.connect(self.scroll_mouse)
        layout.addWidget(self.mouse_scroll_button)

        self.keyboard_label = QLabel('Keyboard Operations:')
        self.keyboard_label.setFont(font)
        layout.addWidget(self.keyboard_label)

        self.keyboard_type_button = QPushButton('Type "Hello, World!"', self)
        self.keyboard_type_button.setFont(font)
        self.keyboard_type_button.clicked.connect(self.type_text)
        layout.addWidget(self.keyboard_type_button)

        self.keyboard_hotkey_button = QPushButton('Press Ctrl+C', self)
        self.keyboard_hotkey_button.setFont(font)
        self.keyboard_hotkey_button.clicked.connect(self.press_hotkey)
        layout.addWidget(self.keyboard_hotkey_button)

        self.record_button = QPushButton('Start Recording', self)
        self.record_button.setFont(font)
        self.record_button.clicked.connect(self.start_recording)
        layout.addWidget(self.record_button)

        self.stop_record_button = QPushButton('Stop Recording', self)
        self.stop_record_button.setFont(font)
        self.stop_record_button.clicked.connect(self.stop_recording)
        layout.addWidget(self.stop_record_button)

        self.play_button = QPushButton('Play Events', self)
        self.play_button.setFont(font)
        self.play_button.clicked.connect(self.play_events)
        layout.addWidget(self.play_button)

        self.save_button = QPushButton('Save Events', self)
        self.save_button.setFont(font)
        self.save_button.clicked.connect(self.save_events)
        layout.addWidget(self.save_button)

        self.load_button = QPushButton('Load Events', self)
        self.load_button.setFont(font)
        self.load_button.clicked.connect(self.load_events)
        layout.addWidget(self.load_button)

        self.setLayout(layout)

    def move_mouse(self):
        pyautogui.moveTo(100, 100, duration=1)

    def click_mouse(self):
        pyautogui.click(100, 100)

    def drag_mouse(self):
        pyautogui.dragTo(200, 200, duration=1)

    def scroll_mouse(self):
        pyautogui.scroll(500)

    def type_text(self):
        # 点击输入框位置以确保其获得焦点
        pyautogui.click(100, 100)
        time.sleep(1)  # 等待焦点稳定
        pyautogui.write('Hello, World!', interval=0.1)

    def press_hotkey(self):
        pyautogui.hotkey('ctrl', 'c')

    def start_recording(self):
        self.system.start_recording()

    def stop_recording(self):
        self.system.stop_recording()

    def play_events(self):
        self.system.play_events()

    def save_events(self):
        filename, _ = QFileDialog.getSaveFileName(self, "Save Events", "", "JSON Files (*.json)")
        if filename:
            self.system.save_events(filename)

    def load_events(self):
        filename, _ = QFileDialog.getOpenFileName(self, "Load Events", "", "JSON Files (*.json)")
        if filename:
            self.system.load_events(filename)

class App(QWidget):
    def __init__(self, system):
        super().__init__()
        self.system = system
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Intelligent Control System')
        self.setGeometry(100, 100, 400, 400)

        layout = QVBoxLayout()

        self.tabs = QTabWidget()
        self.clipboard_monitor_tab = ClipboardMonitorTab(self.system)
        self.mouse_keyboard_simulator_tab = MouseKeyboardSimulatorTab(self.system)
        self.base_converter_tab = BaseConverterTab()
        self.ip_converter_tab = IPConverterTab()  # 新增的IP转换标签页

        self.tabs.addTab(self.clipboard_monitor_tab, "Clipboard Monitor")
        self.tabs.addTab(self.mouse_keyboard_simulator_tab, "Mouse & Keyboard Simulator")
        self.tabs.addTab(self.base_converter_tab, "Base Converter")
        self.tabs.addTab(self.ip_converter_tab, "IP Converter")  # 添加IP转换标签页

        layout.addWidget(self.tabs)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('simulator.ico'))  # 设置应用程序图标
    system = IntelligentControlSystem()
    ex = App(system)
    ex.show()
    sys.exit(app.exec_())